package algorithm.search.binarySearch;

public class Binary {
    public static void main(String[] args) {
        int[] arr = {-12, -4, -3, 5, 9, 40, 60};
        System.out.println(binary(arr, 0, arr.length - 1, 600));

    }

    /**
     * 二分查找法（递归）
     *
     * @param arr
     * @param val
     */
    public static int binary(int[] arr, int left, int right, int val) {
        if (right < left)
            return -1;
        int mid = (left + right) / 2;
        if (val == arr[mid])
            return mid;
        if (val > arr[mid]) {
            return binary(arr, mid + 1, right, val);
        } else {
            return binary(arr, left, mid - 1, val);
        }
    }
}
